package cn.uai.activiti8.approval.service.impl;

import cn.uai.activiti8.approval.cmd.Reject2AnyNodeCmd;
import cn.uai.activiti8.approval.cmd.Reject2ApplyNodeCmd;
import cn.uai.activiti8.approval.cmd.Reject2PreNodeCmd;
import cn.uai.activiti8.approval.service.CommandExecutionService;
import cn.uai.activiti8.dto.Result;
import jakarta.annotation.Resource;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.interceptor.Command;
import org.springframework.stereotype.Service;

/**
 * @desc 命令执行服务
 * @author uai
 * @since 2025/8/11 10:42
 * @version 1.0
 */
@Service
public class CommandExecutionServiceImpl implements CommandExecutionService {

    @Resource
    private ProcessEngine processEngine;

    public <T> T executeCommand(Command<T> command) {
        ProcessEngineConfigurationImpl cfg = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
        return cfg.getCommandExecutor().execute(command);
    }

    public String executeRejectCommand(String taskId, Integer rejectType, String targetActivityId) {
        Command<Void> command = null;
        switch (rejectType) {
            case 1:
                System.out.println("驳回1");
                command = new Reject2PreNodeCmd(taskId);
                break;
            case 2:
                System.out.println("驳回2");
                command = new Reject2ApplyNodeCmd(taskId);
                break;
            case 3:
                System.out.println("驳回3");
                command = new Reject2AnyNodeCmd(taskId, targetActivityId);
                break;
            default:
                break;
        }
        if (null == command) {
            return Result.fail("不支持的驳回操作：" + rejectType);
        }
        executeCommand(command);
        return Result.success();
    }
}
